import axios from 'axios'
import {MessageBox, Message} from 'element-ui'
import cookie from 'js-cookie'
import {Toast} from 'vant';

// 创建axios实例
const service = axios.create({
    // baseURL: 'http://pt.shengxue688.com/shengxue',
    baseURL: 'http://localhost:9098',
    // baseURL: 'http://8.210.112.202:9098',
    timeout: 15000 // 请求超时时间
})

// http request 拦截器
service.interceptors.request.use(
    config => {
        // token 先不处理，后续使用时在完善
        if (cookie.get('Authorization')) {
            config.headers['Authorization'] = cookie.get('Authorization')
        }
        return config
    },
    err => {
        return Promise.reject(err)
    })
// http response 拦截器
service.interceptors.response.use(
    response => {
        if (response.data.code === 208) {
            eventLogin.$emit('loginDialogEvent')
            return
        } else {
            if (response.data.code !== 200) {
                // Message({
                //     message: response.data.message,
                //     type: 'error',
                //     duration: 5 * 1000
                // })
                // alert(response.data.message)
                Toast({
                    message: response.data.message,
                    position: 'top',
                });
                return Promise.reject(response.data)
            } else {
                return response.data
            }
        }
    },
    error => {
        return Promise.reject(error.response)
    })
export default service
